package ui.tempdao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import util.HibernateUtil;
import dao.EgnProduct;
import dao.EgnRstProductCf;
import dao.EgnRstProductFn;
import dao.EgnRstProductRisk;
import dao.EgnRstProductVarmap;

public class EgnProductDao {
	private final static Logger logger = LoggerFactory.getLogger(EgnProductDao.class);

	public static List<EgnProduct> getAllEgnProduct() {
		List<EgnProduct> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnProduct instance");
		Query qr = s.createQuery("from EgnProduct" );

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnProduct> getAllEgnProduct(int rstCount) {
		List<EgnProduct> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnProduct instance");
		Query qr = s.createQuery("from EgnProduct" );
		qr.setMaxResults(rstCount);

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}

//	public static ScrollableResults getTypedEgnProduct(String prodType) {
	public static List<EgnProduct> getTypedEgnProduct(String prodType) {
		List<EgnProduct> rst ;
//		ScrollableResults rst;
		
		
		Session s = HibernateUtil.currentSession();
		logger.debug("Get Typed EgnProduct instance");
		Query qr = s.createQuery("from EgnProduct a where a.productType =:param" );
		qr.setParameter("param", prodType);

		try {
//			rst = qr.scroll();
			rst = qr.list();
			HibernateUtil.closeSession();
			
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnProduct> getTypedEgnProduct(String prodType,int rstCount) {
		List<EgnProduct> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get Typed EgnProduct instance");
		Query qr = s.createQuery("from EgnProduct a where a.productType =:param" );
		qr.setParameter("param", prodType);
		qr.setMaxResults(rstCount);

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}

	}

	public static List<String> getEgnProductTypes() {
		List<String> rst ;
		logger.debug("Get EgnProduct Types");
		Session s = HibernateUtil.currentSession();

		Query qr = s.createQuery("select distinct a.productType from EgnProduct a" );

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}

	public static EgnProduct getEgnProduct(String prodId) {
		EgnProduct rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnProduct instance 11");
		
		Query qr = s.createQuery(" from EgnProduct a where a.productId =:param"	);
		qr.setParameter("param", prodId);
		try {
			rst = (EgnProduct)qr.uniqueResult();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	
	public static List<EgnRstProductCf> getCashFlow(String prodId) {
		List<EgnRstProductCf> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnProduct instance 11");
		
		Query qr = s.createQuery(" from EgnRstProductCf a where a.id.productId =:param"	);
		qr.setParameter("param", prodId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnRstProductVarmap> getVarmap(String prodId) {
		List<EgnRstProductVarmap> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnProduct instance 11");
		
		Query qr = s.createQuery(" from EgnRstProductVarmap a where a.id.productId =:param"	);
		qr.setParameter("param", prodId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnRstProductFn> getFnMeasure(String prodId) {
		List<EgnRstProductFn> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnProduct instance 11");
		
		Query qr = s.createQuery(" from EgnRstProductFn a where a.id.productId =:param"	);
		qr.setParameter("param", prodId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<EgnRstProductRisk> getRisk(String prodId) {
		List<EgnRstProductRisk> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnProduct instance 11");
		
		Query qr = s.createQuery(" from EgnRstProductRisk a where a.id.productId =:param"	);
		qr.setParameter("param", prodId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	
}
